/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import Negocio.Empresa;
import Negocio.Mantenimiento;
import Negocio.Personal;
import Negocio.Vehiculo;
import java.sql.Date;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import Utils.FechaUtil;
import java.util.Map;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class FrmMantenimiento extends javax.swing.JDialog {

    /**
     * Creates new form FrmMantenimiento
     */
    
    private Empresa unaEmpresa;
    private DefaultListModel modelMantenimientos = new DefaultListModel();
     private DefaultListModel modelVehiculos = new DefaultListModel();
    private Vehiculo unVehiculo;   
    private Mantenimiento unMantenimiento;
    private Personal unPersonal;
    
    public FrmMantenimiento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public FrmMantenimiento(java.awt.Frame parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.unaEmpresa = unaEmpresa;
        this.LstVehiculos.setModel(modelVehiculos);
        this.LstMantenimientos.setModel(modelMantenimientos);
        this.cargarVehiculos();
        this.TxtPersonal.setEditable(false);
        
        
    }
    
    
    //mis metodos
    public void cargarVehiculos(){
        
        this.modelVehiculos.clear();
        
        if( this.unaEmpresa.getVehiculosActivos().size() > 0){
            for (Map.Entry vehiculo : this.unaEmpresa.getVehiculosActivos().entrySet()) {
                this.modelVehiculos.addElement((Vehiculo) vehiculo.getValue());
            }
        }else{
            this.modelVehiculos.addElement("No se Encontraron Vehiculos");
        } 
    }
    public void cargarMantenimientos(){    
         this.modelMantenimientos.clear();
         this.unVehiculo.ordenarMantenimientos(this.unVehiculo.getMantenimientos(), 1);
        if(this.unVehiculo.getMantenimientos().size() > 0){
            Iterator it = this.unVehiculo.getMantenimientos().iterator();
            while(it.hasNext()){
                this.modelMantenimientos.addElement((Mantenimiento) it.next());
            }
        }else{
            this.modelMantenimientos.addElement("No se encontraron Mantenimientos");
        }
    }
    
    public void setPersonal(Personal unPersonal){
        this.unPersonal = unPersonal;
        this.TxtPersonal.setText(this.unPersonal.toString());
    }
    
    public void limpiarCampos(){
        this.TxtNotas.setText("");
        this.CmbFecha.setText("");
        this.TxtPersonal.setText("");
        this.unPersonal = null;
        this.unVehiculo = null;
        
    }
    
    public void guardarMantenimiento(){
        try{
           this.CmbFecha.setFormat(3);
            Date unaFecha = Empresa.miUtilidadDeFechas.fechaDb(this.CmbFecha.getText());
            if(this.unMantenimiento== null){ //nuevo
                    this.unaEmpresa.nuevoMantenimiento(this.unVehiculo,this.unPersonal,unaFecha,this.TxtNotas.getText());
            }else{
                this.unaEmpresa.actualizarMantenimiento(this.unMantenimiento,unaFecha,this.unPersonal,this.unVehiculo,this.TxtNotas.getText());
            } 
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(this, "Error: "+ex.getMessage());
            
        }
            
        
    }
    public void cargarCampos(){
        this.TxtNotas.setText(this.unMantenimiento.getNotas());
        this.TxtPersonal.setText(this.unMantenimiento.getEmpleado().toString());
        this.unPersonal = this.unMantenimiento.getEmpleado();
        this.unVehiculo = this.unMantenimiento.getVehiculo();
       
        this.CmbFecha.setText(Empresa.miUtilidadDeFechas.fechaAr(this.unMantenimiento.getFecha()));
                
                
    }
    //Fin Mis Metodos
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        LstVehiculos = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        CmbFecha = new datechooser.beans.DateChooserCombo();
        BtnEliminar = new javax.swing.JButton();
        BtnGuardar = new javax.swing.JButton();
        BtnNuevo = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TxtNotas = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        TxtPersonal = new javax.swing.JTextField();
        BtnPersonal = new javax.swing.JButton();
        BtnCancel = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        LstMantenimientos = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        BtnAceptar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        LstVehiculos.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstVehiculos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstVehiculosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LstVehiculos);

        jLabel1.setText("Fecha");

        jLabel2.setText("Notas");

        BtnEliminar.setText("Eliminar");
        BtnEliminar.setEnabled(false);
        BtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnEliminarActionPerformed(evt);
            }
        });

        BtnGuardar.setText("Guardar");
        BtnGuardar.setEnabled(false);
        BtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnGuardarActionPerformed(evt);
            }
        });

        BtnNuevo.setText("Nuevo");
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        TxtNotas.setColumns(20);
        TxtNotas.setRows(5);
        jScrollPane2.setViewportView(TxtNotas);

        jLabel4.setText("Personal");

        BtnPersonal.setText("...");
        BtnPersonal.setEnabled(false);
        BtnPersonal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnPersonalActionPerformed(evt);
            }
        });

        BtnCancel.setText("Cancelar");
        BtnCancel.setEnabled(false);
        BtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelActionPerformed(evt);
            }
        });

        LstMantenimientos.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstMantenimientos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstMantenimientosMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(LstMantenimientos);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(102, 102, 102)
                                .addComponent(CmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(BtnNuevo)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(BtnGuardar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(BtnEliminar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(BtnCancel)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(TxtPersonal, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BtnPersonal))
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(CmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(TxtPersonal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BtnPersonal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnEliminar)
                    .addComponent(BtnGuardar)
                    .addComponent(BtnNuevo)
                    .addComponent(BtnCancel))
                .addGap(4, 4, 4))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Vehiculos");

        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Mantenimientos");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(BtnAceptar))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(198, 198, 198)
                        .addComponent(jLabel6)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BtnAceptar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        this.BtnGuardar.setEnabled(true);
        this.BtnPersonal.setEnabled(true);
       
        this.BtnCancel.setEnabled(true);
        this.BtnEliminar.setEnabled(true);
        this.BtnNuevo.setEnabled(false);
        this.TxtNotas.setText("");
        this.TxtPersonal.setText("");
        
        
        
        this.limpiarCampos();
    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void BtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnGuardarActionPerformed
        // TODO add your handling code here:
        this.guardarMantenimiento();
        this.cargarMantenimientos();
        this.BtnEliminar.setEnabled(false);
        this.BtnNuevo.setEnabled(true);
        this.BtnGuardar.setEnabled(false);
        this.BtnPersonal.setEnabled(false);        
        this.limpiarCampos();
    }//GEN-LAST:event_BtnGuardarActionPerformed

    private void BtnPersonalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnPersonalActionPerformed
        // TODO add your handling code here:
        FrmFindPersonal ventana = new FrmFindPersonal(this,true, this.unaEmpresa);
        ventana.setVisible(true);
    }//GEN-LAST:event_BtnPersonalActionPerformed

    private void BtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnEliminarActionPerformed
        // TODO add your handling code here:
        if(this.unMantenimiento != null){
            this.unaEmpresa.quitarMantenimiento(this.unMantenimiento);
        }else{
            JOptionPane.showMessageDialog(this, "Debe Seleccionar unMantenimiento");  
        }
        this.cargarMantenimientos();
        
    }//GEN-LAST:event_BtnEliminarActionPerformed

    private void LstVehiculosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstVehiculosMouseClicked
        // TODO add your handling code here:
        this.unVehiculo =  (Vehiculo) this.modelVehiculos.getElementAt(this.LstVehiculos.getSelectedIndex());
        this.cargarMantenimientos();
    }//GEN-LAST:event_LstVehiculosMouseClicked

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnAceptarActionPerformed

    private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelActionPerformed
        // TODO add your handling code here:
        this.BtnCancel.setEnabled(false);
        this.BtnEliminar.setEnabled(false);
        this.BtnGuardar.setEnabled(false);
        this.BtnNuevo.setEnabled(true);
        this.unMantenimiento = null;
        this.unPersonal = null;
        this.TxtPersonal.setText("");        
        
        this.TxtNotas.setText("");
    }//GEN-LAST:event_BtnCancelActionPerformed

    private void LstMantenimientosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstMantenimientosMouseClicked
        // TODO add your handling code here:
        this.unMantenimiento =  (Mantenimiento) this.modelMantenimientos.getElementAt(this.LstMantenimientos.getSelectedIndex());
        this.unPersonal = this.unMantenimiento.getEmpleado();
        this.TxtNotas.setText(this.unMantenimiento.getNotas());
        this.TxtPersonal.setText(this.unPersonal.toString());
        
        this.BtnCancel.setEnabled(true);
        this.BtnEliminar.setEnabled(true);
        this.BtnGuardar.setEnabled(true);
        this.BtnNuevo.setEnabled(false);
    }//GEN-LAST:event_LstMantenimientosMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmMantenimiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmMantenimiento dialog = new FrmMantenimiento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnCancel;
    private javax.swing.JButton BtnEliminar;
    private javax.swing.JButton BtnGuardar;
    private javax.swing.JButton BtnNuevo;
    private javax.swing.JButton BtnPersonal;
    private datechooser.beans.DateChooserCombo CmbFecha;
    private javax.swing.JList LstMantenimientos;
    private javax.swing.JList LstVehiculos;
    private javax.swing.JTextArea TxtNotas;
    private javax.swing.JTextField TxtPersonal;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
